-
Notifications
You must be signed in to change notification settings - Fork 764
Automate user curation #6590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Automate user curation #6590
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
kitsune/users/management/commands/cleanup_expired_users.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from datetime import timedelta | ||
|
||
from django.conf import settings | ||
from django.contrib.auth import get_user_model | ||
from django.core.management.base import BaseCommand | ||
from django.utils import timezone | ||
|
||
from kitsune.users.utils import delete_user_pipeline | ||
|
||
|
||
class Command(BaseCommand): | ||
help = "Delete users who haven't logged in for more than settings.USER_INACTIVITY_DAYS days" | ||
|
||
def handle(self, *args, **options): | ||
User = get_user_model() | ||
expiration_date = timezone.now() - timedelta(days=settings.USER_INACTIVITY_DAYS) | ||
|
||
expired_users = User.objects.filter(last_login__lt=expiration_date) | ||
self.stdout.write(f"Found {expired_users.count()} expired users") | ||
|
||
for user in expired_users: | ||
delete_user_pipeline(user) | ||
self.stdout.write(f"Deleted user {user.username}") | ||
|
||
self.stdout.write( | ||
self.style.SUCCESS(f"Successfully processed {expired_users.count()} expired users") | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
from subprocess import check_call | ||
|
||
import babis | ||
import waffle | ||
from apscheduler.schedulers.blocking import BlockingScheduler | ||
from django.conf import settings | ||
from django.utils import timezone | ||
|
@@ -399,6 +400,22 @@ def job_cleanup_old_account_events(): | |
call_command("cleanup_old_account_events") | ||
|
||
|
||
@scheduled_job( | ||
"cron", | ||
month="*", | ||
day="*", | ||
hour="03", | ||
minute="00", | ||
day_of_week=0, | ||
max_instances=1, | ||
coalesce=True, | ||
skip=(settings.READ_ONLY or not waffle.switch_is_active('cleanup-expired-users')), | ||
) | ||
@babis.decorator(ping_after=settings.DMS_CLEANUP_EXPIRED_USERS) | ||
def job_cleanup_expired_users(): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's add a waffle flag/switch to control when this is enabled through admin. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added. |
||
call_command("cleanup_expired_users") | ||
|
||
|
||
def run(): | ||
try: | ||
schedule.start() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The final success message calls expired_users.count() after users have been deleted, which may result in an incorrect count (likely 0). Consider storing the initial count before processing the deletions and using that stored value in the success message.
Copilot uses AI. Check for mistakes.